﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace VLUSM_DataAccess.Repositories
{
    public class CatalogueRepository : IDisposable
    {
        #region Members
        private MainModelContainer context;
        #endregion

        #region Constructor
        public CatalogueRepository(MainModelContainer context)
        {
            this.context = context;
        }
        #endregion

        public IEnumerable<DVHCCap1> GetDVHCCap1List()
        {
            using (var context = new DonViHanhChinhContainer())
            {
                var result = context.DVHCCap1.ToList();
                result.ForEach(e => context.Detach(e));
                return result;
            }
        }

        public DVHCCap1 GetDVHCCap1ById(int IdFilter)
        {
            using (var context = new DonViHanhChinhContainer())
            {

                DVHCCap1 result = context.DVHCCap1.Where(s => s.Id == IdFilter).First();
                context.Detach(result);
                return result;
            }
        }

        public void SaveDVHCCap1(DVHCCap1 obj)
        {
            using (var context = new DonViHanhChinhContainer())
            {
                try
                {
                    //context.Refresh(RefreshMode.StoreWins,
                    //context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified));
                    context.DVHCCap1.AddObject(obj);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

            }
        }

        public void RemoveDVHCCap1(DVHCCap1 obj)
        {
            using (var context = new DonViHanhChinhContainer())
            {
                try
                {
                    //var result = context.DVHCCap1.Where(p => p.Id == obj.Id).First();
                    var result = context.DVHCCap1.Where(p => p.MaDonVi == obj.MaDonVi).First();
                    context.ObjectStateManager.ChangeObjectState(result, EntityState.Deleted);
                    context.SaveChanges();
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }

        public void RemoveDVHCCap2(DVHCCap2 obj)
        {
            using (var context = new DonViHanhChinhContainer())
            {
                var result = context.DVHCCap2.First(c => c.MaDonVi == obj.MaDonVi);
                context.ObjectStateManager.ChangeObjectState(result, EntityState.Deleted);
                context.SaveChanges();
            }
        }

        public IEnumerable<DVHCCap2> GetDVHCCap2ListByDVHCCap1Id(int IdFilter)
        {
            using (var context = new DonViHanhChinhContainer())
            {
                var result = context.DVHCCap2.Where(s => s.Cap1Id == IdFilter).ToList();
                result.ForEach(e => context.Detach(e));
                return result;
            }
        }

        public DVHCCap2 GetDVHCCap2ById(int IdFilter)
        {
            using (var context = new DonViHanhChinhContainer())
            {
                var result = context.DVHCCap2.Where(s => s.Cap1Id == IdFilter).First();
                context.Detach(result);
                return result;
            }
        }

        public void SaveDVHCCap2(DVHCCap2 obj)
        {
            using (var context = new DonViHanhChinhContainer())
            {
                try
                {
                    //context.Refresh(RefreshMode.StoreWins,
                    //context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified));
                    context.DVHCCap2.AddObject(obj);
                    context.SaveChanges();
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }

        #region IDisposable Members
        public void Dispose()
        {
            if (context != null)
            {
                context.Dispose();
            }
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
